package com.ruoyi.net.service.impl;

import com.ruoyi.common.constant.ElementConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.GraphUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.GlobalCounter;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.net.domain.*;
import com.ruoyi.net.mapper.*;
import com.ruoyi.net.service.IBusiMplstpService;
import com.ruoyi.net.service.INetGroupService;
import com.ruoyi.net.utils.autoIncrement.GlobalIncrementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 业务-mplstp配置Service业务层处理
 *
 * @author net
 * @date 2024-11-26
 */
@Service
public class BusiMplstpServiceImpl implements IBusiMplstpService {
    @Autowired
    INetGroupService netGroupService;
    @Autowired
    BusiMplstpRouteGraphMapper busiMplstpRouteGraphMapper;
    @Autowired
    BusiMplstpRouteElementsMapper busiMplstpRouteElementsMapper;
    @Autowired
    BusiMplstpRouteTableMapper busiMplstpRouteTableMapper;
    @Autowired
    NetElementMapper netElementMapper;
    @Autowired
    ElementPortModeMapper elementPortModeMapper;
    @Autowired
    ElementPortEthMapper elementPortEthMapper;
    @Autowired
    GlobalIncrementService globalIncrementService;
    @Autowired
    BusiElementStatusMapper busiElementStatusMapper;
    @Autowired
    BusiMplstpElementMapper busiMplstpElementMapper;
    @Autowired
    BusiMplstpConstraintMapper busiMplstpConstraintMapper;
    @Autowired
    private BusiMplstpMapper busiMplstpMapper;
    @Autowired
    private BusiMplstpRouteMapper busiMplstpRouteMapper;
    @Autowired
    private ElementBusiMplsOtherMapper elementBusiMplsOtherMapper;
    @Autowired
    private ElementBusiMplsProtectMapper elementBusiMplsProtectMapper;
    @Autowired
    private ElementBusiMplsQosflowMapper elementBusiMplsQosflowMapper;
    @Autowired
    private ElementBusiMplsQoslspMapper elementBusiMplsQoslspMapper;
    @Autowired
    private ElementBusiMplsQoslevelMapper elementBusiMplsQoslevelMapper;
    @Autowired
    private ElementBusiMplsAlarmbfpMapper elementBusiMplsAlarmbfpMapper;
    @Autowired
    private ElementBusiMplsAlarmoamMapper elementBusiMplsAlarmoamMapper;
    @Autowired
    ElementPortMtnEvthMapper elementPortMtnEvthMapper;
    private boolean hasFilterFlag = false;

    public static List<List<NetElementLinkVo>> findTwoShortestArrays(List<List<NetElementLinkVo>> arrays) {
        List<List<NetElementLinkVo>> listList = new ArrayList<>();
        if (arrays == null || arrays.size() < 2) {
            System.out.println("做比较的数组只有一条，直接返回该数组");
            return arrays;
        }

        int minLength1 = Integer.MAX_VALUE;
        int minLength2 = Integer.MAX_VALUE;
        List<NetElementLinkVo> shortestArray1 = null;
        List<NetElementLinkVo> shortestArray2 = null;

        for (List<NetElementLinkVo> array : arrays) {
            int length = array.size();

            if (length < minLength1) {
                minLength2 = minLength1;
                shortestArray2 = shortestArray1;
                minLength1 = length;
                shortestArray1 = array;
            } else if (length < minLength2) {
                minLength2 = length;
                shortestArray2 = array;
            }
        }

        System.out.println("The two shortest arrays are:");
        System.out.println("最短路径: " + shortestArray1.toString());
        System.out.println("次最短路径: " + shortestArray2.toString());
        listList.add(shortestArray1);
        listList.add(shortestArray2);
        return listList;
    }

    /**
     * 查询业务-mplstp配置
     *
     * @param id 业务-mplstp配置主键
     * @return 业务-mplstp配置
     */
    @Override
    public BusiMplstp selectBusiMplstpById(String id) {
        return busiMplstpMapper.selectBusiMplstpById(id);
    }

    /**
     * 查询业务-mplstp配置列表
     *
     * @param busiMplstp 业务-mplstp配置
     * @return 业务-mplstp配置
     */
    @Override
    public List<BusiMplstpListVo> selectBusiMplstpList(BusiMplstp busiMplstp) {
        return busiMplstpMapper.selectBusiMplstpList(busiMplstp);
    }

    /**
     * 新增业务-mplstp配置
     *
     * @param busiMplstpVo 业务-mplstp配置
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertBusiMplstp(BusiMplstpVo busiMplstpVo) {
        String uuid = IdUtils.simpleUUID();
        if (StringUtils.isEmpty(busiMplstpVo.getId())) {
            busiMplstpVo.setId(uuid);
        }
        //  判断表中是否已存在该数据，存在则更新，否则新增
        if (busiMplstpMapper.selectBusiMplstpById(busiMplstpVo.getId()) != null) {
            //更新主表
            int i = busiMplstpMapper.updateBusiMplstp(busiMplstpVo);
            initUpdateBusiMplstpAttachInfo(busiMplstpVo);
            updateBusiMplstpRouteGraph(busiMplstpVo);
            //删除路径附表，重新绑定新数据
            busiMplstpRouteMapper.deleteBusiMplstpRouteByMplstpId(busiMplstpVo.getId());
            if (!CollectionUtils.isEmpty(busiMplstpVo.getBusiMplstpRouteList())) {
                for (BusiMplstpRoute busiMplstpRoute : busiMplstpVo.getBusiMplstpRouteList()) {
                    busiMplstpRoute.setId(IdUtils.simpleUUID());
                    busiMplstpRoute.setMplstpId(busiMplstpVo.getId());
                    busiMplstpRoute.setCreateBy(SecurityUtils.getUsername());
                    busiMplstpRoute.setCreateTime(DateUtils.getNowDate());
                    busiMplstpRoute.setUpdateBy(SecurityUtils.getUsername());
                    busiMplstpRoute.setUpdateTime(DateUtils.getNowDate());
                }
                busiMplstpRouteMapper.batchInsertBusiMplstpRoute(busiMplstpVo.getBusiMplstpRouteList());
            }
            return i;
        } else {
            //初始化新增MPLS-TP业务主表的各附属信息
            initInsertBusiMplstpAttachInfo(busiMplstpVo);
            //生成路由图数据
            insertBusiMplstpRouteGraph(busiMplstpVo);

            if (!CollectionUtils.isEmpty(busiMplstpVo.getBusiMplstpRouteList())) {
                for (BusiMplstpRoute busiMplstpRoute : busiMplstpVo.getBusiMplstpRouteList()) {
                    busiMplstpRoute.setCreateBy(SecurityUtils.getUsername());
                    busiMplstpRoute.setCreateTime(DateUtils.getNowDate());
                    busiMplstpRoute.setUpdateBy(SecurityUtils.getUsername());
                    busiMplstpRoute.setUpdateTime(DateUtils.getNowDate());
                }
                //批量新增路由配置
                busiMplstpRouteMapper.batchInsertBusiMplstpRoute(busiMplstpVo.getBusiMplstpRouteList());
            }

            busiMplstpVo.setCreateBy(SecurityUtils.getUsername());
            busiMplstpVo.setCreateTime(DateUtils.getNowDate());
            busiMplstpVo.setUpdateBy(SecurityUtils.getUsername());
            busiMplstpVo.setUpdateTime(DateUtils.getNowDate());
            busiMplstpVo.setStatus(ElementConstants.STATUS_OFF);
//            busiMplstpVo.setTunnelId(globalIncrementService.getNextTunnelIncrementNumber());
            busiMplstpVo.setStartTunnelBundleId(globalIncrementService.getNextBundleIdIncrementNumber());
            busiMplstpVo.setEndTunnelBundleId(globalIncrementService.getNextBundleIdIncrementNumber());
            return busiMplstpMapper.insertBusiMplstp(busiMplstpVo);
        }
    }

    /**
     * 初始化业务MPLS-TP附属信息
     *
     * @param busiMplstpVo
     */
    @Transactional(rollbackFor = Exception.class)
    void initInsertBusiMplstpAttachInfo(BusiMplstpVo busiMplstpVo) {
        //无保护默认生成一条数据
        if ("无保护".equals(busiMplstpVo.getProtectType())) {
            //调用qos-flow模版数据并新增
            insertBusiMplQosflowCommon(busiMplstpVo, "1");
            //调用qos-level模版数据并新增
            insertBusiMplQoslevelCommon(busiMplstpVo, "1");
            //调用qos-lsp模版数据并新增
            insertBusiMplQoslspCommon(busiMplstpVo, "2");
            //5.27修改记录：源宿megName 保持一致
            if ("OAM".equals(busiMplstpVo.getFaultType())) {
                String megName = "meg" + GlobalCounter.get10RandomToHexString();
                //工作路径-源网元
                insertBusiMplsAlarmoamCommon(busiMplstpVo, "1", busiMplstpVo.getStartElement(),megName,"1");
                //工作路径-宿网元
                insertBusiMplsAlarmoamCommon(busiMplstpVo, "1", busiMplstpVo.getEndElement(),megName,"2");
            }/* else if ("BFD".equals(busiMplstpVo.getFaultType())) {
                //工作路径-源网元
                insertBusiMplsAlarmbfpCommon(busiMplstpVo,"1",busiMplstpVo.getStartElement());
                //工作路径-宿网元
                insertBusiMplsAlarmbfpCommon(busiMplstpVo,"1",busiMplstpVo.getEndElement());
            }*/

        } else if ("1:1保护".equals(busiMplstpVo.getProtectType()) || "1+1保护".equals(busiMplstpVo.getProtectType())) {
            //故障检测类型新增四条数据
            if ("OAM".equals(busiMplstpVo.getFaultType())) {
                String megName = "meg" + GlobalCounter.get10RandomToHexString();
                //工作路径-源网元
                insertBusiMplsAlarmoamCommon(busiMplstpVo, "1", busiMplstpVo.getStartElement(),megName,"1");
                //工作路径-宿网元
                insertBusiMplsAlarmoamCommon(busiMplstpVo, "1", busiMplstpVo.getEndElement(),megName,"2");
                megName = "meg" + GlobalCounter.get10RandomToHexString();
                //保护路径-源网元
                insertBusiMplsAlarmoamCommon(busiMplstpVo, "2", busiMplstpVo.getStartElement(),megName,"1");
                //保护路径-宿网元
                insertBusiMplsAlarmoamCommon(busiMplstpVo, "2", busiMplstpVo.getEndElement(),megName,"2");

            }
            /*else if ("BFD".equals(busiMplstpVo.getFaultType())) {
                //工作路径-源网元
                insertBusiMplsAlarmbfpCommon(busiMplstpVo,"1",busiMplstpVo.getStartElement());
                //工作路径-宿网元
                insertBusiMplsAlarmbfpCommon(busiMplstpVo,"1",busiMplstpVo.getEndElement());
                //保护路径-源网元
                insertBusiMplsAlarmbfpCommon(busiMplstpVo,"2",busiMplstpVo.getStartElement());
                //保护路径-宿网元
                insertBusiMplsAlarmbfpCommon(busiMplstpVo,"2",busiMplstpVo.getEndElement());
            }*/
            //新增2条数据
            //调用qos-flow模版数据并新增
            insertBusiMplQosflowCommon(busiMplstpVo, "1");
            insertBusiMplQosflowCommon(busiMplstpVo, "2");
            //调用qos-level模版数据并新增
            insertBusiMplQoslevelCommon(busiMplstpVo, "1");
            insertBusiMplQoslevelCommon(busiMplstpVo, "2");
            //调用qos-lsp模版数据并新增
            insertBusiMplQoslspCommon(busiMplstpVo, "1");
            insertBusiMplQoslspCommon(busiMplstpVo, "2");
            //调用保护属性模版数据并新增
            ElementBusiMplsProtect elementBusiMplsProtect = elementBusiMplsProtectMapper.selectElementBusiMplsProtectTemplate();
            elementBusiMplsProtect.setId(IdUtils.simpleUUID());
            elementBusiMplsProtect.setMplstpId(busiMplstpVo.getId());
            elementBusiMplsProtect.setUpdateBy(SecurityUtils.getUsername());
            elementBusiMplsProtect.setUpdateTime(DateUtils.getNowDate());
            elementBusiMplsProtect.setCreateBy(SecurityUtils.getUsername());
            elementBusiMplsProtect.setCreateTime(DateUtils.getNowDate());
            elementBusiMplsProtect.setTemplateFlag("N");
            elementBusiMplsProtectMapper.insertElementBusiMplsProtect(elementBusiMplsProtect);
        }
        //调用其他属性模版数据并新增
        ElementBusiMplsOther elementBusiMplsOther = elementBusiMplsOtherMapper.selectElementBusiMplsOtherTemplate();
        elementBusiMplsOther.setId(IdUtils.simpleUUID());
        elementBusiMplsOther.setMplstpId(busiMplstpVo.getId());
        elementBusiMplsOther.setUpdateBy(SecurityUtils.getUsername());
        elementBusiMplsOther.setUpdateTime(DateUtils.getNowDate());
        elementBusiMplsOther.setCreateBy(SecurityUtils.getUsername());
        elementBusiMplsOther.setCreateTime(DateUtils.getNowDate());
        String workLspNameId = GlobalCounter.get10RandomToHexString();
        elementBusiMplsOther.setWorkPositiveLspName(workLspNameId + "_0");
        elementBusiMplsOther.setWorkReverseLspName(workLspNameId + "_1");

        String protectLspNameId = GlobalCounter.get10RandomToHexString();
        elementBusiMplsOther.setProductPositiveLspName(protectLspNameId + "_0");
        elementBusiMplsOther.setProductReverseLspName(protectLspNameId + "_1");
        //设置tunnel为一致
//        String tunnelId= globalIncrementService.getNextTunnelIncrementNumber();
//        elementBusiMplsOther.setProductPositiveTunnelId(tunnelId);
//        elementBusiMplsOther.setProductReverseTunnelId(tunnelId);
//        elementBusiMplsOther.setWorkPositiveTunnelId(tunnelId);
//        elementBusiMplsOther.setWorkReverseTunnelId(tunnelId);

        elementBusiMplsOther.setTemplateFlag("N");
        elementBusiMplsOtherMapper.insertElementBusiMplsOther(elementBusiMplsOther);


    }

    /**
     * 插入Mpls-bfp 公共信息
     *
     * @param busiMplstpVo
     * @param routeType
     * @param elementId
     */
    private void insertBusiMplsAlarmbfpCommon(BusiMplstpVo busiMplstpVo, String routeType, String elementId) {
        ElementBusiMplsAlarmbfp elementBusiMplsAlarmbfp = elementBusiMplsAlarmbfpMapper.selectElementBusiMplsAlarmbfpTemplate();
        elementBusiMplsAlarmbfp.setId(IdUtils.simpleUUID());
        elementBusiMplsAlarmbfp.setRouteType(routeType);
        elementBusiMplsAlarmbfp.setMplstpId(busiMplstpVo.getId());
        elementBusiMplsAlarmbfp.setUpdateBy(SecurityUtils.getUsername());
        elementBusiMplsAlarmbfp.setUpdateTime(DateUtils.getNowDate());
        elementBusiMplsAlarmbfp.setCreateBy(SecurityUtils.getUsername());
        elementBusiMplsAlarmbfp.setCreateTime(DateUtils.getNowDate());
        elementBusiMplsAlarmbfp.setTemplateFlag("N");
        elementBusiMplsAlarmbfp.setId(IdUtils.simpleUUID());
        elementBusiMplsAlarmbfp.setElementId(elementId);
        elementBusiMplsAlarmbfpMapper.insertElementBusiMplsAlarmbfp(elementBusiMplsAlarmbfp);
    }

    /**
     * 插入Mpls-oam 公共信息
     *
     * @param busiMplstpVo
     * @param routeType
     */
    private void insertBusiMplsAlarmoamCommon(BusiMplstpVo busiMplstpVo, String routeType, String elementId,String megName,String elementType) {
        ElementBusiMplsAlarmoam elementBusiMplsAlarmoam = elementBusiMplsAlarmoamMapper.selectElementBusiMplsAlarmoamTemplate();
        elementBusiMplsAlarmoam.setId(IdUtils.simpleUUID());
        //工作路径-源网元
        elementBusiMplsAlarmoam.setRouteType(routeType);
        elementBusiMplsAlarmoam.setElementId(elementId);
        elementBusiMplsAlarmoam.setMplstpId(busiMplstpVo.getId());
        elementBusiMplsAlarmoam.setUpdateBy(SecurityUtils.getUsername());
        elementBusiMplsAlarmoam.setUpdateTime(DateUtils.getNowDate());
        elementBusiMplsAlarmoam.setCreateBy(SecurityUtils.getUsername());
        elementBusiMplsAlarmoam.setCreateTime(DateUtils.getNowDate());
        elementBusiMplsAlarmoam.setTemplateFlag("N");
        elementBusiMplsAlarmoam.setMegName(megName);
        elementBusiMplsAlarmoam.setMeName(globalIncrementService.getNextMeNameIncrementNumber());
        elementBusiMplsAlarmoam.setOamId(globalIncrementService.getNextOamIdIncrementNumber());
        if ("1".equals(elementType)) {
            //修改：对比报文后工作、保护megname保持一致  源、宿倒数第二、三位数取值相反即可
            elementBusiMplsAlarmoam.setMeg("123456/789ABC/1/2/7");
        } else {
            //宿
            elementBusiMplsAlarmoam.setMeg("123456/789ABC/2/1/7");
        }
        elementBusiMplsAlarmoamMapper.insertElementBusiMplsAlarmoam(elementBusiMplsAlarmoam);
    }

    /**
     * 插入MplsQoslsp 公共信息
     *
     * @param busiMplstpVo
     * @param routeType
     */
    private void insertBusiMplQoslspCommon(BusiMplstpVo busiMplstpVo, String routeType) {
        ElementBusiMplsQoslsp elementBusiMplsQoslsp = elementBusiMplsQoslspMapper.selectElementBusiMplsQoslspTemplate();
        elementBusiMplsQoslsp.setId(IdUtils.simpleUUID());
        elementBusiMplsQoslsp.setMplslspName(busiMplstpVo.getBusiName() + "_qoslsp");
        elementBusiMplsQoslsp.setMplstpId(busiMplstpVo.getId());
        elementBusiMplsQoslsp.setRouteType(routeType);
        elementBusiMplsQoslsp.setUpdateBy(SecurityUtils.getUsername());
        elementBusiMplsQoslsp.setUpdateTime(DateUtils.getNowDate());
        elementBusiMplsQoslsp.setCreateBy(SecurityUtils.getUsername());
        elementBusiMplsQoslsp.setCreateTime(DateUtils.getNowDate());
        elementBusiMplsQoslsp.setTemplateFlag("N");
        elementBusiMplsQoslspMapper.insertElementBusiMplsQoslsp(elementBusiMplsQoslsp);
    }

    /**
     * 插入MplsQoslevel 公共信息
     *
     * @param busiMplstpVo
     * @param routeType
     */
    private void insertBusiMplQoslevelCommon(BusiMplstpVo busiMplstpVo, String routeType) {
        ElementBusiMplsQoslevel elementBusiMplsQoslevel = elementBusiMplsQoslevelMapper.selectElementBusiMplsQoslevelTemplate();
        elementBusiMplsQoslevel.setId(IdUtils.simpleUUID());
        elementBusiMplsQoslevel.setMplstpId(busiMplstpVo.getId());
        elementBusiMplsQoslevel.setRouteType(routeType);
        elementBusiMplsQoslevel.setUpdateBy(SecurityUtils.getUsername());
        elementBusiMplsQoslevel.setUpdateTime(DateUtils.getNowDate());
        elementBusiMplsQoslevel.setCreateBy(SecurityUtils.getUsername());
        elementBusiMplsQoslevel.setCreateTime(DateUtils.getNowDate());
        elementBusiMplsQoslevel.setTemplateFlag("N");
        elementBusiMplsQoslevelMapper.insertElementBusiMplsQoslevel(elementBusiMplsQoslevel);
    }

    /**
     * 插入MplsQosflow 公共信息
     *
     * @param busiMplstpVo
     * @param routeType
     */
    void insertBusiMplQosflowCommon(BusiMplstpVo busiMplstpVo, String routeType) {
        ElementBusiMplsQosflow templateElementBusiMplsQosflow = elementBusiMplsQosflowMapper.selectElementBusiMplsQosflowTemplate();
        templateElementBusiMplsQosflow.setId(IdUtils.simpleUUID());
        templateElementBusiMplsQosflow.setMplstpId(busiMplstpVo.getId());
        templateElementBusiMplsQosflow.setRouteType(routeType);
        templateElementBusiMplsQosflow.setUpdateBy(SecurityUtils.getUsername());
        templateElementBusiMplsQosflow.setUpdateTime(DateUtils.getNowDate());
        templateElementBusiMplsQosflow.setCreateBy(SecurityUtils.getUsername());
        templateElementBusiMplsQosflow.setCreateTime(DateUtils.getNowDate());
        templateElementBusiMplsQosflow.setTemplateFlag("N");
        elementBusiMplsQosflowMapper.insertElementBusiMplsQosflow(templateElementBusiMplsQosflow);
    }

    /**
     * 初始化更新业务MPLS-TP附属信息
     *
     * @param busiMplstpVo
     */
    @Transactional(rollbackFor = Exception.class)
    void initUpdateBusiMplstpAttachInfo(BusiMplstpVo busiMplstpVo) {
        //先删除附属表信息，再重新插入
        elementBusiMplsQosflowMapper.deleteElementBusiMplsQosflowByMplstpId(busiMplstpVo.getId());
        elementBusiMplsQoslevelMapper.deleteElementBusiMplsQoslevelByMplstpId(busiMplstpVo.getId());
        elementBusiMplsQoslspMapper.deleteElementBusiMplsQoslspByMplstpId(busiMplstpVo.getId());
        elementBusiMplsAlarmbfpMapper.deleteElementBusiMplsAlarmbfpByMplstpId(busiMplstpVo.getId());
        elementBusiMplsAlarmoamMapper.deleteElementBusiMplsAlarmoamByMplstpId(busiMplstpVo.getId());
        elementBusiMplsOtherMapper.deleteElementBusiMplsOtherByMplstpId(busiMplstpVo.getId());
        elementBusiMplsProtectMapper.deleteElementBusiMplsProtectByMplstpId(busiMplstpVo.getId());
        //删除单站业务表
        busiMplstpElementMapper.deleteBusiMplstpElementByMplstpId(busiMplstpVo.getId());
        busiElementStatusMapper.deleteBusiElementStatusBySrtpId(busiMplstpVo.getId());

        initInsertBusiMplstpAttachInfo(busiMplstpVo);

    }

    /**
     * 修改业务-mplstp配置
     *
     * @param busiMplstp 业务-mplstp配置
     * @return 结果
     */
    @Override
    public int updateBusiMplstp(BusiMplstp busiMplstp) {
        busiMplstp.setUpdateBy(SecurityUtils.getUsername());
        busiMplstp.setUpdateTime(DateUtils.getNowDate());
        return busiMplstpMapper.updateBusiMplstp(busiMplstp);
    }

    /**
     * 批量删除业务-mplstp配置
     *
     * @param ids 需要删除的业务-mplstp配置主键
     * @return 结果
     */
    @Override
    public int deleteBusiMplstpByIds(String[] ids) {
        return busiMplstpMapper.deleteBusiMplstpByIds(ids);
    }

    /**
     * 删除业务-mplstp配置信息
     *
     * @param id 业务-mplstp配置主键
     * @return 结果
     */
    @Override
    public int deleteBusiMplstpById(String id) {
        return busiMplstpMapper.deleteBusiMplstpById(id);
    }

    /**
     * 更新业务-mplstp配置状态为已提交
     *
     * @param busiMplstp
     * @return
     */
    @Override
    public int updateBusiMplstpStatus(BusiMplstp busiMplstp) {
        busiMplstp.setUpdateBy(SecurityUtils.getUsername());
        busiMplstp.setUpdateTime(DateUtils.getNowDate());
        return busiMplstpMapper.updateBusiMplstp(busiMplstp);
    }

    @Override
    public List<BusiMplstpListVo2> selectBusiMplstpEleList2(BusiMplstpListVo busiMplstp) {
        List<BusiMplstpListVo2> busiMplstpListVo2s = busiMplstpMapper.selectBusiMplstpEleList2(busiMplstp);
        List<BusiMplstpListVo2> startEleListVos = caculateAttachInfo3(busiMplstp, busiMplstpListVo2s);
        return startEleListVos;
    }

    /**
     * 组装页面业务管理-mpls-tp列表上
     * @param busiMplstp
     * @param busiMplstpListVo2s
     * @return
     */
    private List<BusiMplstpListVo2> caculateAttachInfo3(BusiMplstpListVo busiMplstp, List<BusiMplstpListVo2> busiMplstpListVo2s) {
        BusiMplstpRouteTable busiMplstpRouteTable = new BusiMplstpRouteTable();
        busiMplstpRouteTable.setServiceFlag("N");
        for (BusiMplstpListVo2 busiMplstpListVo : busiMplstpListVo2s) {
            //工作、保护
            busiMplstpRouteTable.setRouteType(busiMplstpListVo.getRouteType());
            busiMplstpRouteTable.setMplstpId(busiMplstpListVo.getMplstpId());
            busiMplstpRouteTable.setRouteDirection("1");
            List<BusiMplstpRouteTableListVo> tableListVos = busiMplstpRouteTableMapper.selectBusiMplstpRouteTableList(busiMplstpRouteTable);
            //设置正向值   工作的有问题
            //前后端达成一致   start为入  end为出
            //1对于源网元，只有源端口源标签，有正向下一跳地址，无反向下一跳地址；出端口、出标签从路由表中获取
            //2对于宿网元，只有宿端口宿标签，无正向下一跳地址，有反向下一跳地址；出端口、出标签从路由表中获取
            if (!CollectionUtils.isEmpty(tableListVos) && tableListVos.size() > 1) {
                if ("1".equals(busiMplstpListVo.getElementType())) {
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos.get(0).getPortId());
                    busiMplstpListVo.setPortMode(endElementPortMode.getPortMode());
                    //正向出端口
                    busiMplstpListVo.setPositiveEndPortName(endElementPortMode.getPortName());
                    busiMplstpListVo.setPositiveEndLabel(tableListVos.get(0).getLabel());
                    //获取下一个网元的ip  下一跳地址，只有出端口存在才有
                    ElementPortEth elementPortEth = new ElementPortEth();
                    elementPortEth.setElementId(tableListVos.get(1).getElementId());
                    ElementPortMode elementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos.get(1).getPortId());
                    elementPortEth.setPortName(elementPortMode.getPortName());
                    if (!"veth".equals(elementPortMode.getPortMode())) {
                        List<ElementPortEth> elementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);
                        if (!CollectionUtils.isEmpty(elementPortEths)) {
                            busiMplstpListVo.setPositiveIp(elementPortEths.get(0).getIp());
                        }
                    }else {
                        //虚拟口时查询 mtnveth接口表
                        ElementPortMtnEvth elementPortMtnEvth = elementPortMtnEvthMapper.selectElementPortMtnEvthByTerminalName(tableListVos.get(1).getElementId(), elementPortMode.getPortName());
                        if (elementPortMtnEvth != null) {
                            busiMplstpListVo.setPositiveIp(elementPortMtnEvth.getIp());
                            busiMplstpListVo.setMask(elementPortMtnEvth.getMask());
                        }
                    }



                } else if ("2".equals(busiMplstpListVo.getElementType())) {
                    //2对于宿网元来说，只有入端口入标签，入端口、入标签从路由表中获取
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos.get(tableListVos.size() - 1).getPortId());
                    busiMplstpListVo.setPortMode(endElementPortMode.getPortMode());
                    busiMplstpListVo.setPositiveStartPortName(endElementPortMode.getPortName());
                    busiMplstpListVo.setPositiveStartLabel(tableListVos.get(tableListVos.size() - 1).getLabel());
                } else if ("3".equals(busiMplstpListVo.getElementType())) {
                    List<BusiMplstpRouteTableListVo> crossCollects = tableListVos.stream().filter(tableListVo -> tableListVo.getElementId().equals(busiMplstp.getElementId())).collect(Collectors.toList());
                    //过站数据
                    //1设置标签值
                    busiMplstpListVo.setPositiveStartLabel(crossCollects.get(0).getLabel());
                    busiMplstpListVo.setPositiveEndLabel(crossCollects.get(1).getLabel());
                    //通过关联查询到eth表端口  和下一跳ip

                    BusiMplstpRouteTable routeTableParam = new BusiMplstpRouteTable();
                    routeTableParam.setElementId(crossCollects.get(0).getElementId());
//                routeTableParam.setoruteDirection("1");
                    //设置端口名称
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(crossCollects.get(1).getPortId());
                    busiMplstpListVo.setPositiveEndPortName(endElementPortMode.getPortName());
                    ElementPortMode startElementPortMode = elementPortModeMapper.selectElementPortModeById(crossCollects.get(0).getPortId());
                    busiMplstpListVo.setPositiveStartPortName(startElementPortMode.getPortName());
//                    busiMplstpListVo.setPortMode(startElementPortMode.getPortMode());

                    //3设置下一条ip
                    //4获取下一网元信息
//                NetElement netElement = netElementMapper.selectNetElementById(tableListVos.get(tableListVos.size() - 1).getElementId());
//                busiMplstpEleListVo.setPositiveIp(netElement.getIp());
                    ElementPortEth elementPortEth = new ElementPortEth();
                    elementPortEth.setElementId(tableListVos.get(tableListVos.size() - 1).getElementId());
                    ElementPortMode elementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos.get(tableListVos.size() - 1).getPortId());
                    elementPortEth.setPortName(elementPortMode.getPortName());
                    if (!"veth".equals(elementPortMode.getPortMode())) {
                        List<ElementPortEth> elementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);
                        if (!CollectionUtils.isEmpty(elementPortEths)) {
                            busiMplstpListVo.setPositiveIp(elementPortEths.get(0).getIp());
                        }
                    }else{
                        //设置虚拟口下一跳ip ,查询 mtnveth接口表
                        ElementPortMtnEvth elementPortMtnEvth = elementPortMtnEvthMapper.selectElementPortMtnEvthByTerminalName(tableListVos.get(tableListVos.size() - 1).getElementId(), elementPortMode.getPortName());
                        if (elementPortMtnEvth != null) {
                            busiMplstpListVo.setPositiveIp(elementPortMtnEvth.getIp());
                            busiMplstpListVo.setMask(elementPortMtnEvth.getMask());
                        }
                    }
                }

            }
            busiMplstpRouteTable.setRouteDirection("2");
            List<BusiMplstpRouteTableListVo> tableListVos2 = busiMplstpRouteTableMapper.selectBusiMplstpRouteTableList(busiMplstpRouteTable);

            //设置反向值
            if (!CollectionUtils.isEmpty(tableListVos2) && tableListVos2.size() > 1) {
                if ("1".equals(busiMplstpListVo.getElementType())) {
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos2.get(tableListVos2.size() - 1).getPortId());
                    busiMplstpListVo.setPortMode(endElementPortMode.getPortMode());
                    busiMplstpListVo.setReverseStartPortName(endElementPortMode.getPortName());
                    busiMplstpListVo.setReverseStartLabel(tableListVos2.get(tableListVos2.size() - 1).getLabel());

                } else if ("2".equals(busiMplstpListVo.getElementType())) {
                    //2对于宿网元来说，只有入端口入标签，入端口、入标签从路由表中获取
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos2.get(0).getPortId());
                    busiMplstpListVo.setPortMode(endElementPortMode.getPortMode());
                    //设置反向出端口出标签
                    busiMplstpListVo.setReverseEndPortName(endElementPortMode.getPortName());
                    busiMplstpListVo.setReverseEndLabel(tableListVos2.get(0).getLabel());
                    //获取下一个网元的ip 下一跳地址，只有出端口存在才有
                    ElementPortEth elementPortEth = new ElementPortEth();
                    elementPortEth.setElementId(tableListVos2.get(1).getElementId());
                    ElementPortMode elementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos2.get(1).getPortId());
                    elementPortEth.setPortName(elementPortMode.getPortName());
                    if (!"veth".equals(elementPortMode.getPortMode())){
                        List<ElementPortEth> elementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);
                        if (!CollectionUtils.isEmpty(elementPortEths)) {
                            busiMplstpListVo.setReverseIp(elementPortEths.get(0).getIp());
                        }
                    }else {
                        //虚拟口时查询 mtnveth接口表
                        ElementPortMtnEvth elementPortMtnEvth = elementPortMtnEvthMapper.selectElementPortMtnEvthByTerminalName(tableListVos2.get(1).getElementId(), elementPortMode.getPortName());
                        if (elementPortMtnEvth != null) {
                            busiMplstpListVo.setReverseIp(elementPortMtnEvth.getIp());
                            busiMplstpListVo.setMask(elementPortMtnEvth.getMask());
                        }
                    }


                } else if ("3".equals(busiMplstpListVo.getElementType())) {
                    //过站数据
                    //1设置标签值
                    List<BusiMplstpRouteTableListVo> crossCollects = tableListVos2.stream().filter(tableListVo -> tableListVo.getElementId().equals(busiMplstp.getElementId())).collect(Collectors.toList());
                    //过站数据
                    //1设置标签值
                    //对于过站数据  1设置标签值
                    busiMplstpListVo.setReverseStartLabel(crossCollects.get(0).getLabel());
                    busiMplstpListVo.setReverseEndLabel(crossCollects.get(1).getLabel());
                    //通过关联查询到eth表端口  和下一跳ip

                    BusiMplstpRouteTable routeTableParam = new BusiMplstpRouteTable();
                    routeTableParam.setElementId(crossCollects.get(0).getElementId());
                    //设置端口名称
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(crossCollects.get(0).getPortId());

                    ElementPortMode startElementPortMode = elementPortModeMapper.selectElementPortModeById(crossCollects.get(1).getPortId());
                    busiMplstpListVo.setReverseEndPortName(startElementPortMode.getPortName());
                    busiMplstpListVo.setReverseStartPortName(endElementPortMode.getPortName());
//                    busiMplstpListVo.setPortMode(startElementPortMode.getPortMode());
                    //3设置下一条ip
                    //4获取下一网元信息
//                NetElement netElement = netElementMapper.selectNetElementById(tableListVos2.get(tableListVos2.size() - 1).getElementId());
//                busiMplstpEleListVo.setReverseIp(netElement.getIp());
                    ElementPortEth elementPortEth = new ElementPortEth();
                    elementPortEth.setElementId(tableListVos2.get(tableListVos2.size() - 1).getElementId());
                    ElementPortMode elementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos2.get(tableListVos2.size() - 1).getPortId());
                    elementPortEth.setPortName(elementPortMode.getPortName());
                    if (!"veth".equals(elementPortMode.getPortMode())){
                        List<ElementPortEth> elementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);
                        //设置以太吓一跳ip
                        if (!CollectionUtils.isEmpty(elementPortEths)) {
                            busiMplstpListVo.setReverseIp(elementPortEths.get(0).getIp());
                        }
                    }else {
                        //设置虚拟口下一跳ip ,查询 mtnveth接口表
                        ElementPortMtnEvth elementPortMtnEvth = elementPortMtnEvthMapper.selectElementPortMtnEvthByTerminalName(tableListVos2.get(tableListVos2.size() - 1).getElementId(), elementPortMode.getPortName());
                        if (elementPortMtnEvth != null) {
                            busiMplstpListVo.setReverseIp(elementPortMtnEvth.getIp());
                            busiMplstpListVo.setMask(elementPortMtnEvth.getMask());
                        }
                    }
                }

            }
        }
        return busiMplstpListVo2s;
    }

    /**
     * 查询网管-业务管理mplstp列表
     *
     * @param busiMplstp 业务-mplstp配置
     * @return 业务-mplstp配置集合
     */
    @Override
    public List<BusiMplstpEleListVo> selectBusiMplstpEleList(BusiMplstpListVo busiMplstp) {
        List<BusiMplstpEleListVo> resultList = new ArrayList<>();
        BusiMplstpListVo busiMplstpStart = new BusiMplstpListVo();
        busiMplstpStart.setStartElement(busiMplstp.getElementId());
        List<BusiMplstpListVo> startList = busiMplstpMapper.selectBusiMplstpEleList(busiMplstpStart);
        List<BusiMplstpEleListVo> startEleListVos = caculateAttachInfo(busiMplstp, startList, "start");
        resultList.addAll(startEleListVos);

        BusiMplstpListVo busiMplstpEnd = new BusiMplstpListVo();
        busiMplstpEnd.setEndElement(busiMplstp.getElementId());
        List<BusiMplstpListVo> endList = busiMplstpMapper.selectBusiMplstpEleList(busiMplstpEnd);
        List<BusiMplstpEleListVo> endEleListVos = caculateAttachInfo(busiMplstp, endList, "end");
        resultList.addAll(endEleListVos);
        //查询过站数据
        BusiMplstpListVo busiMplstpMid = new BusiMplstpListVo();
        busiMplstpMid.setCrossIds(busiMplstp.getElementId());
        List<BusiMplstpListVo> crossList = busiMplstpMapper.selectBusiMplstpEleList(busiMplstpMid);
        List<BusiMplstpEleListVo> middleEleListVos = caculateAttachInfo2(busiMplstp, crossList, "middle");
        resultList.addAll(middleEleListVos);
        return resultList;
    }

    /**
     * 计算网管-mpls列表附上信息
     *
     * @param busiMplstp
     * @param mplstpListVos
     * @param type
     * @return
     */
    List<BusiMplstpEleListVo> caculateAttachInfo(BusiMplstpListVo busiMplstp, List<BusiMplstpListVo> mplstpListVos, String type) {
        List<BusiMplstpEleListVo> resultList = new ArrayList<>();

        BusiMplstpRouteTable busiMplstpRouteTable = new BusiMplstpRouteTable();
        busiMplstpRouteTable.setRouteType("1");
        busiMplstpRouteTable.setServiceFlag("N");
        for (BusiMplstpListVo busiMplstpListVo : mplstpListVos) {
            busiMplstpRouteTable.setMplstpId(busiMplstpListVo.getId());
            busiMplstpRouteTable.setRouteDirection("1");
            List<BusiMplstpRouteTableListVo> tableListVos = busiMplstpRouteTableMapper.selectBusiMplstpRouteTableList(busiMplstpRouteTable);

            BusiMplstpEleListVo busiMplstpEleListVo = new BusiMplstpEleListVo();
            busiMplstpEleListVo.setUpdateBy(busiMplstpListVo.getUpdateBy());
            busiMplstpEleListVo.setUpdateTime(busiMplstpListVo.getUpdateTime());
            busiMplstpEleListVo.setCreateBy(busiMplstpListVo.getCreateBy());
            busiMplstpEleListVo.setCreateTime(busiMplstpListVo.getCreateTime());
            busiMplstpEleListVo.setLspId(busiMplstpListVo.getLspId());
            busiMplstpEleListVo.setId(busiMplstpListVo.getId());
            busiMplstpEleListVo.setBusiName(busiMplstpListVo.getBusiName());
            busiMplstpEleListVo.setFaultType(busiMplstpListVo.getFaultType());
            busiMplstpEleListVo.setStatus(busiMplstpListVo.getStatus());
            busiMplstpEleListVo.setDelFlag(busiMplstpListVo.getDelFlag());
            busiMplstpEleListVo.setProtectType(busiMplstpListVo.getProtectType());
            busiMplstpEleListVo.setStartLsrId(busiMplstpListVo.getStartLspId());
            busiMplstpEleListVo.setEndLsrId(busiMplstpListVo.getEndLspId());
            busiMplstpEleListVo.setStartElement(busiMplstpListVo.getStartElement());
            busiMplstpEleListVo.setEndElement(busiMplstpListVo.getEndElement());
            busiMplstpEleListVo.setTunnelId(busiMplstpListVo.getTunnelId());
            //取自其他属性表
            busiMplstpEleListVo.setWorkPositiveTunnelId(busiMplstpListVo.getWorkPositiveTunnelId());
            busiMplstpEleListVo.setWorkReverseTunnelId(busiMplstpListVo.getWorkReverseTunnelId());
            busiMplstpEleListVo.setWorkPositiveLspName(busiMplstpListVo.getWorkPositiveLspName());
            busiMplstpEleListVo.setWorkReverseLspName(busiMplstpListVo.getWorkReverseLspName());
            busiMplstpEleListVo.setResultType(type);

            //设置正向值

            //前后端达成一致   start为入  end为出
            //1对于源网元，只有源端口源标签，有正向下一跳地址，无反向下一跳地址；出端口、出标签从路由表中获取
            //2对于宿网元，只有宿端口宿标签，无正向下一跳地址，有反向下一跳地址；出端口、出标签从路由表中获取
            if (!CollectionUtils.isEmpty(tableListVos) && tableListVos.size() > 1) {
                if ("start".equals(type)) {
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos.get(0).getPortId());
                    //正向入端口
                    busiMplstpEleListVo.setPositiveEndPortName(endElementPortMode.getPortName());
                    busiMplstpEleListVo.setPositiveEndLabel(tableListVos.get(0).getLabel());
                    //获取下一个网元的ip  下一跳地址，只有出端口存在才有
                    ElementPortEth elementPortEth = new ElementPortEth();
                    elementPortEth.setElementId(tableListVos.get(1).getElementId());
                    elementPortEth.setPortName(elementPortModeMapper.selectElementPortModeById(tableListVos.get(1).getPortId()).getPortName());
                    List<ElementPortEth> elementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);
                    if (!CollectionUtils.isEmpty(elementPortEths)) {
                        busiMplstpEleListVo.setPositiveIp(elementPortEths.get(0).getIp());
                    }
                } else if ("end".equals(type)) {
                    //2对于宿网元来说，只有入端口入标签，入端口、入标签从路由表中获取
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos.get(tableListVos.size() - 1).getPortId());
                    busiMplstpEleListVo.setPositiveStartPortName(endElementPortMode.getPortName());
                    busiMplstpEleListVo.setPositiveStartLabel(tableListVos.get(tableListVos.size() - 1).getLabel());
                }

            }
            busiMplstpRouteTable.setRouteDirection("2");
            List<BusiMplstpRouteTableListVo> tableListVos2 = busiMplstpRouteTableMapper.selectBusiMplstpRouteTableList(busiMplstpRouteTable);

            //设置反向值
            if (!CollectionUtils.isEmpty(tableListVos2) && tableListVos2.size() > 1) {
                if ("start".equals(type)) {
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos2.get(tableListVos2.size() - 1).getPortId());
                    busiMplstpEleListVo.setReverseStartPortName(endElementPortMode.getPortName());
                    busiMplstpEleListVo.setReverseStartLabel(tableListVos2.get(tableListVos2.size() - 1).getLabel());

                } else if ("end".equals(type)) {
                    //2对于宿网元来说，只有入端口入标签，入端口、入标签从路由表中获取
                    ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(tableListVos2.get(0).getPortId());
                    //设置反向出端口出标签
                    busiMplstpEleListVo.setReverseEndPortName(endElementPortMode.getPortName());
                    busiMplstpEleListVo.setReverseEndLabel(tableListVos2.get(0).getLabel());
                    //获取下一个网元的ip 下一跳地址，只有出端口存在才有
                    ElementPortEth elementPortEth = new ElementPortEth();
                    elementPortEth.setElementId(tableListVos.get(1).getElementId());
                    elementPortEth.setPortName(elementPortModeMapper.selectElementPortModeById(tableListVos.get(1).getPortId()).getPortName());
                    List<ElementPortEth> elementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);
                    if (!CollectionUtils.isEmpty(elementPortEths)) {
                        busiMplstpEleListVo.setReverseIp(elementPortEths.get(0).getIp());
                    }
                }

            }
            resultList.add(busiMplstpEleListVo);
        }
        return resultList;
    }

    /**
     * 计算网管-mpls列表附上信息
     *
     * @param mplstpListVos
     * @param type
     * @return
     */
    List<BusiMplstpEleListVo> caculateAttachInfo2(BusiMplstpListVo busiMplstpParam, List<BusiMplstpListVo> mplstpListVos, String type) {
        List<BusiMplstpEleListVo> resultList = new ArrayList<>();

        BusiMplstpRouteTable busiMplstpRouteTable = new BusiMplstpRouteTable();
        busiMplstpRouteTable.setRouteType("1");
        busiMplstpRouteTable.setServiceFlag("N");
        for (BusiMplstpListVo busiMplstpListVo : mplstpListVos) {
            //组装过站网元数组
//            String [] crossIds = busiMplstpListVo.getCrossIds().split(",");
            //左上查询条件
            busiMplstpRouteTable.setMplstpId(busiMplstpListVo.getId());
            busiMplstpRouteTable.setRouteDirection("1");
            //目前是单个过站   只有三个节点适用！！！！！！
//            busiMplstpRouteTable.setElementId(busiMplstpListVo.getCrossIds());
            List<BusiMplstpRouteTableListVo> tableListVos = busiMplstpRouteTableMapper.selectBusiMplstpRouteTableList(busiMplstpRouteTable);
            List<BusiMplstpRouteTableListVo> crossCollects = tableListVos.stream().filter(tableListVo -> tableListVo.getElementId().equals(busiMplstpParam.getElementId())).collect(Collectors.toList());

            BusiMplstpEleListVo busiMplstpEleListVo = new BusiMplstpEleListVo();
            busiMplstpEleListVo.setUpdateBy(busiMplstpListVo.getUpdateBy());
            busiMplstpEleListVo.setUpdateTime(busiMplstpListVo.getUpdateTime());
            busiMplstpEleListVo.setCreateBy(busiMplstpListVo.getCreateBy());
            busiMplstpEleListVo.setCreateTime(busiMplstpListVo.getCreateTime());
            busiMplstpEleListVo.setLspId(busiMplstpListVo.getLspId());
            busiMplstpEleListVo.setId(busiMplstpListVo.getId());
            busiMplstpEleListVo.setBusiName(busiMplstpListVo.getBusiName());
            busiMplstpEleListVo.setFaultType(busiMplstpListVo.getFaultType());
            busiMplstpEleListVo.setStatus(busiMplstpListVo.getStatus());
            busiMplstpEleListVo.setDelFlag(busiMplstpListVo.getDelFlag());
            busiMplstpEleListVo.setProtectType(busiMplstpListVo.getProtectType());
            busiMplstpEleListVo.setStartLsrId(busiMplstpListVo.getStartLspId());
            busiMplstpEleListVo.setEndLsrId(busiMplstpListVo.getEndLspId());
            busiMplstpEleListVo.setStartElement(busiMplstpListVo.getStartElement());
            busiMplstpEleListVo.setEndElement(busiMplstpListVo.getEndElement());
            //取自其他属性表
            busiMplstpEleListVo.setWorkPositiveTunnelId(busiMplstpListVo.getWorkPositiveTunnelId());
            busiMplstpEleListVo.setWorkReverseTunnelId(busiMplstpListVo.getWorkReverseTunnelId());
            busiMplstpEleListVo.setWorkPositiveLspName(busiMplstpListVo.getWorkPositiveLspName());
            busiMplstpEleListVo.setWorkReverseLspName(busiMplstpListVo.getWorkReverseLspName());
            busiMplstpEleListVo.setResultType(type);
            busiMplstpEleListVo.setCrossIds(busiMplstpListVo.getCrossIds());
            String lspNameId = GlobalCounter.get10RandomToHexString();
            busiMplstpEleListVo.setWorkPositiveLspName(lspNameId + "_0");
            busiMplstpEleListVo.setWorkReverseLspName(lspNameId + "_1");
            //设置正向值
            if (!CollectionUtils.isEmpty(tableListVos) && tableListVos.size() > 1) {
                //对于过站数据  1设置标签值
                busiMplstpEleListVo.setPositiveStartLabel(crossCollects.get(0).getLabel());
                busiMplstpEleListVo.setPositiveEndLabel(crossCollects.get(1).getLabel());
                //通过关联查询到eth表端口  和下一跳ip

                BusiMplstpRouteTable routeTableParam = new BusiMplstpRouteTable();
                routeTableParam.setElementId(crossCollects.get(0).getElementId());
//                routeTableParam.setoruteDirection("1");
                //设置端口名称
                ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(crossCollects.get(1).getPortId());
                busiMplstpEleListVo.setPositiveEndPortName(endElementPortMode.getPortName());
                ElementPortMode startElementPortMode = elementPortModeMapper.selectElementPortModeById(crossCollects.get(0).getPortId());
                busiMplstpEleListVo.setPositiveStartPortName(startElementPortMode.getPortName());
                //3设置下一条ip
                //4获取下一网元信息
//                NetElement netElement = netElementMapper.selectNetElementById(tableListVos.get(tableListVos.size() - 1).getElementId());
//                busiMplstpEleListVo.setPositiveIp(netElement.getIp());

                ElementPortEth elementPortEth = new ElementPortEth();
                elementPortEth.setElementId(tableListVos.get(tableListVos.size() - 1).getElementId());
                elementPortEth.setPortName(elementPortModeMapper.selectElementPortModeById(tableListVos.get(tableListVos.size() - 1).getPortId()).getPortName());

                List<ElementPortEth> elementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);

                if (!CollectionUtils.isEmpty(elementPortEths)) {
                    busiMplstpEleListVo.setPositiveIp(elementPortEths.get(0).getIp());
                }
            }
            busiMplstpRouteTable.setRouteDirection("2");
            List<BusiMplstpRouteTableListVo> tableListVos2 = busiMplstpRouteTableMapper.selectBusiMplstpRouteTableList(busiMplstpRouteTable);
            List<BusiMplstpRouteTableListVo> crossCollects2 = tableListVos.stream().filter(tableListVo -> tableListVo.getElementId().equals(busiMplstpParam.getElementId())).collect(Collectors.toList());

            //设置反向值
            if (!CollectionUtils.isEmpty(crossCollects2) && crossCollects2.size() > 1) {
                //对于过站数据  1设置标签值
                busiMplstpEleListVo.setReverseStartLabel(crossCollects2.get(1).getLabel());
                busiMplstpEleListVo.setReverseEndLabel(crossCollects2.get(0).getLabel());
                //通过关联查询到eth表端口  和下一跳ip

                BusiMplstpRouteTable routeTableParam = new BusiMplstpRouteTable();
                routeTableParam.setElementId(crossCollects2.get(0).getElementId());
                //设置端口名称
                ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(crossCollects2.get(0).getPortId());
                busiMplstpEleListVo.setReverseEndPortName(endElementPortMode.getPortName());
                ElementPortMode startElementPortMode = elementPortModeMapper.selectElementPortModeById(crossCollects2.get(1).getPortId());
                busiMplstpEleListVo.setReverseStartPortName(startElementPortMode.getPortName());
                //3设置下一条ip
                //4获取下一网元信息
//                NetElement netElement = netElementMapper.selectNetElementById(tableListVos2.get(tableListVos2.size() - 1).getElementId());
//                busiMplstpEleListVo.setReverseIp(netElement.getIp());
                ElementPortEth elementPortEth = new ElementPortEth();
                elementPortEth.setElementId(tableListVos2.get(tableListVos2.size() - 1).getElementId());
                elementPortEth.setPortName(elementPortModeMapper.selectElementPortModeById(tableListVos2.get(tableListVos2.size() - 1).getPortId()).getPortName());
                List<ElementPortEth> elementPortEths = elementPortEthMapper.selectElementPortEthList(elementPortEth);

                if (!CollectionUtils.isEmpty(elementPortEths)) {
                    busiMplstpEleListVo.setReverseIp(elementPortEths.get(0).getIp());
                }
            }
            resultList.add(busiMplstpEleListVo);
        }
        return resultList;
    }

    /**
     * mplstp生成路由图表
     *
     * @param
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertBusiMplstpRouteGraph(BusiMplstpVo busiMplstpVo) {
        NetGroup netGroup = netGroupService.selectNetGroupById(busiMplstpVo.getStartElement());
        //源宿节点的父id:
        String parentId = netGroup.getParentId();
        Map<String, Integer> indexMap = new HashMap<String, Integer>();
        Set<String> idsUnique = new HashSet<>();
        List<TopoVo> topoVos1 = netGroupService.selectTopo(parentId);
        List<TopoVo> topoVos = new ArrayList<>();

        //获取总节点数组 并过滤eth  PortMode 为enable 2.11修改
        //3.16修改获取总节点数组 并过滤eth、虚拟(小颗粒)  PortMode 为enable或者veth
        for (TopoVo topoVo : topoVos1) {
            if (!CollectionUtils.isEmpty(topoVo.getLinkData())) {
                String startPortId = topoVo.getLinkData().get(0).getStartPortId();
                String endPortId = topoVo.getLinkData().get(0).getEndPortId();
                ElementPortMode startElementPortMode = elementPortModeMapper.selectElementPortModeById(startPortId);
                ElementPortMode endElementPortMode = elementPortModeMapper.selectElementPortModeById(endPortId);
                if (!"disable".equals(startElementPortMode.getPortMode()) && !"disable".equals(endElementPortMode.getPortMode())) {
                    topoVos.add(topoVo);
                }
            }
        }
        for (TopoVo topoVo : topoVos) {
            idsUnique.add(topoVo.getToId());
            idsUnique.add(topoVo.getFromId());
        }
        List<String> ids = new ArrayList<>(idsUnique);
        for (int i = 0; i < ids.size(); i++) {
            indexMap.put(ids.get(i), i);
        }
        Map<String, String> elementMap = new HashMap<String, String>();
        //构造双向线段
        for (TopoVo topoVo : topoVos) {
            elementMap.put(topoVo.getFromId(), topoVo.getFromName());
            elementMap.put(topoVo.getToId(), topoVo.getToName());

        }
        //获取总节点个数
        GraphUtil.Graph graph = new GraphUtil.Graph(ids.size());
        //构建路由对象
        for (int i = 0; i < topoVos.size(); i++) {
            graph.addEdge(indexMap.get(topoVos.get(i).getFromId()), indexMap.get(topoVos.get(i).getToId()));
            graph.addEdge(indexMap.get(topoVos.get(i).getToId()), indexMap.get(topoVos.get(i).getFromId()));

        }
        List<List<Integer>> paths = graph.allPathsSourceToDestination(indexMap.get(busiMplstpVo.getStartElement()), indexMap.get(busiMplstpVo.getEndElement()));
        Set<List<String>> listIdsUnq = new HashSet<>();
        List<List<String>> listIds = new ArrayList<>();

        for (List<Integer> path : paths) {
            List<String> list = new ArrayList<>();
            for (Integer integer : path) {
                list.add(ids.get(integer));
            }
            listIdsUnq.add(list);
            listIds.add(list);
        }
        //去重的线路set转list
        List<List<String>> filterList = new ArrayList<>(listIdsUnq);
        List<List<String>> filterList2 = new ArrayList<>();

        //是否有过滤约束
        hasFilterConstrant(busiMplstpVo);
        //todo 将所有的路由列举，添加过滤策略，过滤后的结果（工作、保护）入库
        List<List<NetElementLinkVo>> resultByFilter = filterRouteLineByConstraint(listIdsUnq, topoVos, busiMplstpVo);
        //获取两条最短路径
        List<List<NetElementLinkVo>> twoShortestInfos = findTwoShortestArrays(resultByFilter);
        for (List<NetElementLinkVo> netElementLinkVos : twoShortestInfos) {
            List<String> filters = new ArrayList<>();
            for (NetElementLinkVo netElementLinkVo : netElementLinkVos) {
                filters.add(netElementLinkVo.getStartElementId());
                filters.add(netElementLinkVo.getEndElementId());
            }
            List<String> toList = new ArrayList<>(filters);
            LinkedHashSet<String> linkedHashSet = new LinkedHashSet<>(filters);
            List<String> arrayList = new ArrayList<>(linkedHashSet);
            filterList2.add(arrayList);
        }
        //声明两条最短路径
        List<List<String>> finalRouteResultNoFilter = new ArrayList<>();
        if (hasFilterFlag) {
            finalRouteResultNoFilter = GraphUtil.findTwoShortestArrays(filterList2);
        } else {
            finalRouteResultNoFilter = GraphUtil.findTwoShortestArrays(listIds);
        }


        /**
         *
         * 1、如果是无保护存工作路径  直接取第一个最小数组,如果是1:1则取整个数组（两条最短路径），
         * 生成保护存工作路径、保护路径，然后构造fromId toId linkData
         * 2、将最短路径分隔成有向的n条线段
         * 3、将所有线段与topo图对比，获取属性全部信息
         * 4、将结果属性全部信息存入数据库
         */

        if ("无保护".equals(busiMplstpVo.getProtectType())) {
            if (finalRouteResultNoFilter.size() < 1 || twoShortestInfos.size() < 1) {
                throw new ServiceException("计算条件不足");
            }
            List<List<String>> workRoutes = new ArrayList<List<String>>();
            //获取有序路由  按照节点顺序
            List<String> shortestRoute = finalRouteResultNoFilter.get(0);
            workRoutes.add(shortestRoute);
            //插入一条保护路径网元信息（附带顺序）  插入路由图表数据
            insertBusiMplstpRouteElementsBatch(busiMplstpVo.getId(), shortestRoute, elementMap, "1");
            //业务设备状态表设置初始状态
            updateBusiMplsElementStatus(busiMplstpVo.getId(), shortestRoute);
            List<List<String>> allLines = GraphUtil.cutRouteToLine(shortestRoute);
            System.out.println("所有线段: " + allLines);

            //生成路由图
            if (hasFilterFlag) {
                insertBusiMplstpRouteGraphCommon2(busiMplstpVo, twoShortestInfos.get(0), "1");
            } else {
                insertBusiMplstpRouteGraphCommon(busiMplstpVo, topoVos, allLines, "1");
            }
            //找到当前业务下所有网元，并入库至业务设备状态表并设置初始状态
            initBusiMplsElementStatus(busiMplstpVo.getId(), workRoutes);
            //生成路由表
            busiMplstpVo.setGolabelLabel(globalIncrementService.getNextOutLabelIncrementNumber());
            insertBusiMplstpRouteTableCommon(busiMplstpVo, allLines, twoShortestInfos.get(0), "1", "1", "Y");
            insertBusiMplstpRouteTableCommon(busiMplstpVo, allLines, twoShortestInfos.get(0), "1", "1", "N");
            busiMplstpVo.setGolabelLabel(globalIncrementService.getNextOutLabelIncrementNumber());
            insertBusiMplstpRouteTableCommon(busiMplstpVo, allLines, twoShortestInfos.get(0), "1", "2", "Y");
            insertBusiMplstpRouteTableCommon(busiMplstpVo, allLines, twoShortestInfos.get(0), "1", "2", "N");
            //设置过站网元，多个以逗号分割
            setCrossElement(busiMplstpVo, shortestRoute);
            //2.26  新增单站业务表-工作（逻辑：工作路径每个网元生成一条待激活单站业务数据）
            insertBusiMplsElement("1", busiMplstpVo, shortestRoute);

        } else if ("1:1保护".equals(busiMplstpVo.getProtectType()) || "1+1保护".equals(busiMplstpVo.getProtectType())) {
            //如果是1:1保护 则取整个数组（两条最短路径），生成保护存工作路径、保护路径，然后构造fromId toId linkData
            if (finalRouteResultNoFilter.size() < 2 || twoShortestInfos.size()<2) {
                throw new ServiceException("计算条件不足");
            }
            //线路1所有线段
            //插入一条保护路径网元信息（附带顺序）
            insertBusiMplstpRouteElementsBatch(busiMplstpVo.getId(), finalRouteResultNoFilter.get(0), elementMap, "1");
            List<List<String>> workAllLines1 = GraphUtil.cutRouteToLine(finalRouteResultNoFilter.get(0));
            //生成路由图
            if (hasFilterFlag) {
                insertBusiMplstpRouteGraphCommon2(busiMplstpVo, twoShortestInfos.get(0), "1");
            } else {
                insertBusiMplstpRouteGraphCommon(busiMplstpVo, topoVos, workAllLines1, "1");
            }
            //2.26  新增单站业务表-工作（逻辑：工作路径每个网元生成一条待激活单站业务数据）
            insertBusiMplsElement("1", busiMplstpVo, finalRouteResultNoFilter.get(0));
            //线路2所有线段
            //插入一条保护路径网元信息（附带顺序）
            insertBusiMplstpRouteElementsBatch(busiMplstpVo.getId(), finalRouteResultNoFilter.get(1), elementMap, "2");
            //找到当前业务下所有网元，并入库至业务设备状态表并设置初始状态
            initBusiMplsElementStatus(busiMplstpVo.getId(), finalRouteResultNoFilter);

            List<List<String>> workAllLines2 = GraphUtil.cutRouteToLine(finalRouteResultNoFilter.get(1));
            //生成路由图
            if (hasFilterFlag) {
                insertBusiMplstpRouteGraphCommon2(busiMplstpVo, twoShortestInfos.get(1), "2");
            } else {
                insertBusiMplstpRouteGraphCommon(busiMplstpVo, topoVos, workAllLines2, "2");
            }
            busiMplstpVo.setGolabelLabel(globalIncrementService.getNextOutLabelIncrementNumber());
            insertBusiMplstpRouteTableCommon(busiMplstpVo, workAllLines1, twoShortestInfos.get(0), "1", "1", "Y");
            insertBusiMplstpRouteTableCommon(busiMplstpVo, workAllLines1, twoShortestInfos.get(0), "1", "1", "N");
            busiMplstpVo.setGolabelLabel(globalIncrementService.getNextOutLabelIncrementNumber());
            insertBusiMplstpRouteTableCommon(busiMplstpVo, workAllLines1, twoShortestInfos.get(0), "1", "2", "Y");
            insertBusiMplstpRouteTableCommon(busiMplstpVo, workAllLines1, twoShortestInfos.get(0), "1", "2", "N");
            busiMplstpVo.setGolabelLabel(globalIncrementService.getNextOutLabelIncrementNumber());

            insertBusiMplstpRouteTableCommon(busiMplstpVo, workAllLines2, twoShortestInfos.get(1), "2", "1", "Y");
            insertBusiMplstpRouteTableCommon(busiMplstpVo, workAllLines2, twoShortestInfos.get(1), "2", "1", "N");
            busiMplstpVo.setGolabelLabel(globalIncrementService.getNextOutLabelIncrementNumber());

            insertBusiMplstpRouteTableCommon(busiMplstpVo, workAllLines2, twoShortestInfos.get(1), "2", "2", "Y");
            insertBusiMplstpRouteTableCommon(busiMplstpVo, workAllLines2, twoShortestInfos.get(1), "2", "2", "N");
            //2.26  新增单站业务表-保护 （逻辑：保护路径每个网元生成一条待激活单站业务数据，源宿有两条）
            insertBusiMplsElement("2", busiMplstpVo, finalRouteResultNoFilter.get(1));
        }
    }

    /**
     * 是否有过滤约束
     *
     * @param busiMplstpVo
     * @return
     */
    boolean hasFilterConstrant(BusiMplstpVo busiMplstpVo) {
        BusiMplstpConstraint busiMplstpConstraint = new BusiMplstpConstraint();
        busiMplstpConstraint.setMplstpId(busiMplstpVo.getId());
        List<BusiMplstpConstraint> busiMplstpConstraints = busiMplstpConstraintMapper.selectBusiMplstpConstraintList(busiMplstpConstraint);
        hasFilterFlag = !CollectionUtils.isEmpty(busiMplstpConstraints);
        return hasFilterFlag;
    }

    /**
     * 1.拼装所有路由结果  2.根据约束条件过滤  3.根据约束条件过滤后的结果，生成路由图
     *
     * @param allLines2
     * @param busiMplstpVo
     * @return
     */
    private List<List<NetElementLinkVo>> filterRouteLineByConstraint(Set<List<String>> allLines2, List<TopoVo> topoVos, BusiMplstpVo busiMplstpVo) {
        List<List<String>> allLines = new ArrayList<>(allLines2);
        List<TopoVo> topoVoSort = new ArrayList<>();
        List<MidVo> midLines = new ArrayList<>();
        //将计算的路由线路转换为topo
        for (int i = 0; i < allLines.size(); i++) {
            List<List<NetElementLinkVo>> subLines = new ArrayList<>();
            MidVo midVo = new MidVo();
            List<List<String>> lines = GraphUtil.cutRouteToLine(allLines.get(i));
            midVo.setElements(allLines.get(i));
            for (int j = 0; j < lines.size(); j++) {
                List<NetElementLinkVo> netElementLinkVos = new ArrayList<>();
                for (int k = 0; k < topoVos.size(); k++) {
                    //正向对应
                    if ((lines.get(j).get(0).equals(topoVos.get(k).getFromId()) && lines.get(j).get(1).equals(topoVos.get(k).getToId())) ) {
//                    if ((lines.get(j).get(0).equals(topoVos.get(k).getFromId()) && lines.get(j).get(1).equals(topoVos.get(k).getToId())) || (lines.get(j).get(1).equals(topoVos.get(k).getFromId()) && lines.get(j).get(0).equals(topoVos.get(k).getToId()))) {
                        topoVoSort.add(topoVos.get(k));
                        netElementLinkVos.add(topoVos.get(k).getLinkData().get(0));
                    }else if ((lines.get(j).get(1).equals(topoVos.get(k).getFromId()) && lines.get(j).get(0).equals(topoVos.get(k).getToId()))){
                        //反向对应  需重组link对象
                        //网元信息
                        NetElementLinkVo netElementLinkVo = new NetElementLinkVo();
                        netElementLinkVo.setStartElementId(topoVos.get(k).getToId());
                        netElementLinkVo.setStartName(topoVos.get(k).getToName());
                        netElementLinkVo.setEndElementId(topoVos.get(k).getFromId());
                        netElementLinkVo.setEndName(topoVos.get(k).getFromName());
                        //端口信息
                        netElementLinkVo.setStartPortId(topoVos.get(k).getLinkData().get(0).getEndPortId());
                        netElementLinkVo.setStartPort(topoVos.get(k).getLinkData().get(0).getEndPort());
                        netElementLinkVo.setEndPortId(topoVos.get(k).getLinkData().get(0).getStartPortId());
                        netElementLinkVo.setEndPort(topoVos.get(k).getLinkData().get(0).getStartPort());
                        netElementLinkVos.add(netElementLinkVo);
                    }
                }
                subLines.add(netElementLinkVos);
            }
            midVo.setSubLines(subLines);
            midLines.add(midVo);
        }
        //所有路由结果
        List<List<NetElementLinkVo>> combinLineInfo = new ArrayList<>();
        for (int i = 0; i < midLines.size(); i++) {
            List<List<NetElementLinkVo>> combinations = combine(midLines.get(i).getSubLines());
            for (List<NetElementLinkVo> combinations1 : combinations) {
                for (NetElementLinkVo netElementLinkVo : combinations1) {
                    netElementLinkVo.setElements(midLines.get(i).getElements());
                }
            }
            combinLineInfo.addAll(combinations);
        }
        List<List<NetElementLinkVo>> finalResult = new ArrayList<>();
        //处理网元过滤
        BusiMplstpConstraint busiMplstpConstraint = new BusiMplstpConstraint();
        busiMplstpConstraint.setMplstpId(busiMplstpVo.getId());
        busiMplstpConstraint.setConstraintType("网元");
        List<BusiMplstpConstraint> busiMplstpConstraints = busiMplstpConstraintMapper.selectBusiMplstpConstraintList(busiMplstpConstraint);
        if (!CollectionUtils.isEmpty(busiMplstpConstraints)) {
            for (List<NetElementLinkVo> lineObjs : combinLineInfo) {
                for (BusiMplstpConstraint constraint : busiMplstpConstraints) {
                    List<NetElementLinkVo> oneRoute = new ArrayList<>();
                    //处理网元包含、不包含逻辑
                    if ("包含".equals(constraint.getConstraintStrategy())) {
                        for (NetElementLinkVo lineObj : lineObjs) {
                            if (lineObj.getStartElementId().equals(constraint.getElementId()) || lineObj.getEndElementId().equals(constraint.getElementId())) {
                                oneRoute.add(lineObj);
//                                finalResult.add(lineObjs);

                            }
                        }
                    } else {
                        for (NetElementLinkVo lineObj : lineObjs) {
                            if (!lineObj.getStartElementId().equals(constraint.getElementId()) && !lineObj.getEndElementId().equals(constraint.getElementId())) {
                                oneRoute.add(lineObj);
//                                finalResult.add(lineObjs);
                            }
                        }
                    }
                    if (oneRoute.size() > 0) {
                        finalResult.add(oneRoute);
                    }
                }
            }
        } else {
            finalResult = combinLineInfo;
        }

        List<List<NetElementLinkVo>> finalResult2 = new ArrayList<>();
        //处理端口过滤
        busiMplstpConstraint.setConstraintType("端口");
        List<BusiMplstpConstraint> busiMplstpConstraints2 = busiMplstpConstraintMapper.selectBusiMplstpConstraintList(busiMplstpConstraint);
        if (!CollectionUtils.isEmpty(busiMplstpConstraints2)) {
            for (List<NetElementLinkVo> lineObjs : finalResult) {
                List<NetElementLinkVo> oneRoute = new ArrayList<>();
                for (BusiMplstpConstraint constraint : busiMplstpConstraints2) {
                    //处理端口包含、不包含逻辑
                    if ("包含".equals(constraint.getConstraintStrategy())) {
                        for (NetElementLinkVo lineObj : lineObjs) {
                            if (lineObj.getStartPortId().equals(constraint.getPortId()) || lineObj.getEndPortId().equals(constraint.getPortId())) {
//                                oneRoute.add(lineObj);
                                finalResult2.add(lineObjs);
                            }
                        }
                    } else {
                        for (NetElementLinkVo lineObj : lineObjs) {
                            if (!lineObj.getStartPortId().equals(constraint.getPortId()) && !lineObj.getEndPortId().equals(constraint.getPortId())) {
//                                oneRoute.add(lineObj);
                                finalResult2.add(lineObjs);
                            }
                        }
                    }

                }
               /* if (oneRoute.size() > 0) {
                    finalResult2.add(oneRoute);
                }*/
            }
        } else {
            finalResult2 = finalResult;
        }

        return finalResult2;
    }

    /**
     * 找到当前业务下所有不重复网元，并入库至业务设备状态表并设置初始状态
     *
     * @param mplstpId         主业务id
     * @param finalRouteResult 最短两条线集合
     */
    private void initBusiMplsElementStatus(String mplstpId, List<List<String>> finalRouteResult) {
        HashSet<String> uniqueElement = new HashSet<>();
        //找出不重复的网元
        for (List<String> oneRoute : finalRouteResult) {
            for (String elementId : oneRoute) {
                uniqueElement.add(elementId);
            }
        }
        //入库至业务设备状态表并设置初始状态
        for (String elementId : uniqueElement) {
            BusiElementStatus busiElementStatus = new BusiElementStatus();
            busiElementStatus.setElementId(elementId);
            busiElementStatus.setBusiId(mplstpId);
            busiElementStatus.setId(IdUtils.simpleUUID());
            busiElementStatus.setStatus("0");
            busiElementStatus.setBusiType(ElementConstants.BUSI_MPLS);
            busiElementStatus.setCreateBy(SecurityUtils.getUsername());
            busiElementStatus.setCreateTime(DateUtils.getNowDate());
            busiElementStatusMapper.insertBusiElementStatus(busiElementStatus);
        }
    }

    /**
     * 新增单站业务表
     *
     * @param busiMplstpVo
     * @param shortestRoute routeType 1:工作  2：保护
     */
    private void insertBusiMplsElement(String routeType, BusiMplstpVo busiMplstpVo, List<String> shortestRoute) {
        //无保护  生成条数与网元数一致
        for (int i = 0; i < shortestRoute.size(); i++) {
            BusiMplstpElement busiMplstpElement = new BusiMplstpElement();
            busiMplstpElement.setId(IdUtils.simpleUUID());
            busiMplstpElement.setMplstpId(busiMplstpVo.getId());
            busiMplstpElement.setElementId(shortestRoute.get(i));
            busiMplstpElement.setCreateBy(SecurityUtils.getUsername());
            busiMplstpElement.setCreateTime(DateUtils.getNowDate());
            busiMplstpElement.setRouteType(routeType);
            if (i == 0) {
                //1=：源,2=：宿,3=：,过=站
                busiMplstpElement.setElementType("1");
            } else if (i == shortestRoute.size() - 1) {
                busiMplstpElement.setElementType("2");
            } else {
                busiMplstpElement.setElementType("3");
            }
            String lspName = GlobalCounter.get10RandomToHexString();
            busiMplstpElement.setPositiveLspName(lspName + "_0");
            busiMplstpElement.setReverseLspName(lspName + "_1");
            busiMplstpElement.setPositiveTunnelId(globalIncrementService.getNextTunnelIncrementNumber());//???用的太多   不唯一性
            busiMplstpElement.setReverseTunnelId(globalIncrementService.getNextTunnelIncrementNumber());
            busiMplstpElement.setLspId(globalIncrementService.getNextLspIdIncrementNumber());
            busiMplstpElementMapper.insertBusiMplstpElement(busiMplstpElement);
        }
    }

    /**
     * 业务设备状态表设置初始状态
     *
     * @param mplstpId
     * @param shortestRoute 最短路径网元集合
     */
    private void updateBusiMplsElementStatus(String mplstpId, List<String> shortestRoute) {
        //业务设备状态表设置初始状态
        for (String elementId : shortestRoute) {
            BusiElementStatus busiElementStatus = new BusiElementStatus();
            busiElementStatus.setElementId(elementId);
            busiElementStatus.setBusiId(mplstpId);
            List<BusiElementStatus> busiElementStatuses = busiElementStatusMapper.selectBusiElementStatusList(busiElementStatus);
            //避免重复插入
            if (busiElementStatuses.size() == 0) {
                busiElementStatus.setId(IdUtils.simpleUUID());
                busiElementStatus.setStatus("0");
                busiElementStatus.setBusiType(ElementConstants.BUSI_MPLS);
                busiElementStatusMapper.insertBusiElementStatus(busiElementStatus);
            }
        }

    }

    /**
     * 设置过站网元
     *
     * @param busiMplstpVo
     * @param shortestRouteWithOrder 最短路径含顺序
     */
    private void setCrossElement(BusiMplstpVo busiMplstpVo, List<String> shortestRouteWithOrder) {
        String crossEleIds = "";
        //获取过站数据
        List<String> corssIds = new ArrayList<>();
        for (int i = 0; i < shortestRouteWithOrder.size(); i++) {
            if (i != 0 && i != shortestRouteWithOrder.size() - 1) {
                corssIds.add(shortestRouteWithOrder.get(i));
            }
        }
        //拼接过站数据
        for (int i = 0; i < corssIds.size(); i++) {
            if (i == 0 || i == corssIds.size() - 1) {
                crossEleIds += corssIds.get(i);
            } else {
                crossEleIds = crossEleIds + "," + corssIds.get(i);
            }
        }
        BusiMplstp busiMplstp = new BusiMplstp();
        busiMplstp.setId(busiMplstpVo.getId());
        busiMplstp.setCrossIds(crossEleIds);
        updateBusiMplstp(busiMplstp);
    }

    /**
     * 插入Mplstp路由表公共方法
     *
     * @param busiMplstpVo
     * @param allLines
     * @param routeType
     */
    private void insertBusiMplstpRouteTableCommon(BusiMplstpVo busiMplstpVo, List<List<String>> allLines, List<NetElementLinkVo> links, String routeType, String routeDirection, String flag) {
        //是否有路由约束
        if (hasFilterFlag) {
            //正向
            if ("1".equals(routeDirection)) {
                for (int i = 0; i < links.size(); i++) {
                    NetElementLinkVo link = links.get(i);
                    insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                    insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));

                  /*  if (links.size()==1){
                        insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));

                    }else {
                        //如果是两条及以上   头尾可以设置为一致
                        if (i == 0) {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        } else if (i == links.size() - 1) {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()) + 1);
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        } else {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()) + 1);
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));

                        }
                    }*/
                }

            } else {
                //反向
                for (int i = links.size(); i > 0; i--) {
                    NetElementLinkVo link = links.get(i - 1);
                    insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                    insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType,  link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));

                 /*   if (links.size()==1){
                        insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));

                    }else{
                        //如果是两条及以上   头尾可以设置为一致
                        if (i == 1) {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        } else if (i == links.size()) {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType,  link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()) + 1);
                        } else {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, link.getEndElementId(), link.getEndPortId(), link.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()) + 1);
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType,  link.getStartElementId(), link.getStartPortId(), link.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        }

                    }*/
                }
            }


        } else {
            BusiMplstpRouteGraph busiMplstpRouteGraph = new BusiMplstpRouteGraph();
            busiMplstpRouteGraph.setMplstpId(busiMplstpVo.getId());
            busiMplstpRouteGraph.setRouteType(routeType);
            //查询路由图 包含构造的逆向数据
            List<BusiMplstpRouteGraph> busiMplstpRouteGraphs = busiMplstpRouteGraphMapper.selectBusiMplstpRouteGraphList(busiMplstpRouteGraph);

            List<BusiMplstpRouteGraph> filterResult = new ArrayList<>();
            //按照顺序分割好的线段与路由图对比取 link属性
            for (int i = 0; i < allLines.size(); i++) {
                for (int j = 0; j < busiMplstpRouteGraphs.size(); j++) {
                    if (allLines.get(i).get(0).equals(busiMplstpRouteGraphs.get(j).getFromId()) && allLines.get(i).get(1).equals(busiMplstpRouteGraphs.get(j).getToId())) {
                        filterResult.add(busiMplstpRouteGraphs.get(j));
                    }
                }
            }
            //正向  filterResult为线集合  1-2  2-3 3-4
            if ("1".equals(routeDirection)) {
                for (int i = 0; i < filterResult.size(); i++) {
                    BusiMplstpRouteGraph graph = filterResult.get(i);
                    //如果只有一条线  两个点直接设置为一致
                    if (filterResult.size() == 1) {
                        insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getFromId(), graph.getStartPortId(), graph.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getToId(), graph.getEndPortId(), graph.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));

                    } else {
                        //如果是两条及以上   头尾可以设置为一致
                        if (i == 0) {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getFromId(), graph.getStartPortId(), graph.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getToId(), graph.getEndPortId(), graph.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        } else if (i == filterResult.size() - 1) {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getFromId(), graph.getStartPortId(), graph.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()) + 1);
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getToId(), graph.getEndPortId(), graph.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        } else {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getFromId(), graph.getStartPortId(), graph.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()) + 1);
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getToId(), graph.getEndPortId(), graph.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));

                        }
                    }

                }
            } else {
                //反向  filterResult为线集合 1-2 2-3  3-4
                for (int i = filterResult.size(); i > 0; i--) {
                    BusiMplstpRouteGraph graph = filterResult.get(i - 1);
                    //如果只有一条线  两个点标签直接设置为一致
                    if (filterResult.size() == 1) {
                        insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getToId(), graph.getEndPortId(), graph.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getFromId(), graph.getStartPortId(), graph.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                    } else {
                        //如果是两条及以上   头尾可以设置为一致
                        if (i == 1) {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getToId(), graph.getStartPortId(), graph.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getFromId(), graph.getEndPortId(), graph.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        } else if (i == filterResult.size()) {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getToId(), graph.getStartPortId(), graph.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getFromId(), graph.getEndPortId(), graph.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()) + 1);
                        } else {
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getToId(), graph.getStartPortId(), graph.getStartPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()) + 1);
                            insertBusiMplstpRouteTable(busiMplstpVo, routeDirection, routeType, graph.getFromId(), graph.getEndPortId(), graph.getEndPort(), flag, Integer.parseInt(busiMplstpVo.getGolabelLabel()));
                        }
                    }
                }
            }
        }
    }

    private void insertBusiMplstpRouteTable(BusiMplstpVo busiMplstpVo, String routeDirection, String routeType, String elementId, String portId, String portName, String flag, int seq) {
        BusiMplstpRouteTable busiMplstpRouteTable = new BusiMplstpRouteTable();
        busiMplstpRouteTable.setId(IdUtils.simpleUUID());
        busiMplstpRouteTable.setServiceFlag(flag);
        busiMplstpRouteTable.setMplstpId(busiMplstpVo.getId());
        busiMplstpRouteTable.setRouteDirection(routeDirection);
        busiMplstpRouteTable.setRouteType(routeType);
        busiMplstpRouteTable.setElementId(elementId);
        busiMplstpRouteTable.setPortId(portId);
        if (!"Y".equals(flag)) {
            busiMplstpRouteTable.setPortName("VP-1");
        }
        busiMplstpRouteTable.setLabel(String.valueOf(seq));//自增
        busiMplstpRouteTable.setTerminalLabel("");
        busiMplstpRouteTable.setVlanId("");
        busiMplstpRouteTable.setCreateBy(SecurityUtils.getUsername());
        busiMplstpRouteTable.setCreateTime(DateUtils.getNowDate());
        busiMplstpRouteTable.setUpdateBy(SecurityUtils.getUsername());
        busiMplstpRouteTable.setUpdateTime(DateUtils.getNowDate());
        busiMplstpRouteTableMapper.insertBusiMplstpRouteTable(busiMplstpRouteTable);
    }

    /**
     * 插入Mplstp路由图公共方法
     *
     * @param busiMplstpVo
     * @param topoVos      过滤类型后的所有线段含端口信息
     * @param allLines     包含顺序的节点线段
     * @param routeType
     */
    public void insertBusiMplstpRouteGraphCommon(BusiMplstpVo busiMplstpVo, List<TopoVo> topoVos, List<List<String>> allLines, String routeType) {
        //将所有线段与topo图对比，获取属性全部信息
        String pathId2 = IdUtils.simpleUUID();
        List<TopoVo> topoVoSort = new ArrayList<>();
        //将topo图按照线段排序
        for (int i = 0; i < allLines.size(); i++) {
            for (int j = 0; j < topoVos.size(); j++) {
                if ((allLines.get(i).get(0).equals(topoVos.get(j).getFromId()) && allLines.get(i).get(1).equals(topoVos.get(j).getToId())) || (allLines.get(i).get(1).equals(topoVos.get(j).getFromId()) && allLines.get(i).get(0).equals(topoVos.get(j).getToId()))) {
                    topoVoSort.add(topoVos.get(j));
                }
            }
        }
        for (int j = 0; j < topoVoSort.size(); j++) {
            for (int i = 0; i < allLines.size(); i++) {
                //用返回最短路径结果与topo图对比，获取属性全部信息
                if ((topoVoSort.get(j).getFromId().equals(allLines.get(i).get(0)) && topoVoSort.get(j).getToId().equals(allLines.get(i).get(1))) || (topoVoSort.get(j).getFromId().equals(allLines.get(i).get(1)) && topoVoSort.get(j).getToId().equals(allLines.get(i).get(0)))) {
                    //避免重复插入脏数据
                    List<BusiMplstpRouteGraph> busiMplstpRouteGraphs = validbusiMplstpRouteGraph(busiMplstpVo, topoVoSort.get(j), pathId2);
                    if (CollectionUtils.isEmpty(busiMplstpRouteGraphs)) {
                        BusiMplstpRouteGraph testRouteEntity = new BusiMplstpRouteGraph();
                        if (StringUtils.isEmpty(testRouteEntity.getId())) {
                            testRouteEntity.setId(IdUtils.simpleUUID());
                        }
                        testRouteEntity.setRouteType(routeType);
                        //线路id  用于区分同一个MplstpId下 的多条线路
                        testRouteEntity.setPathId(pathId2);
                        //主表主键
                        testRouteEntity.setMplstpId(busiMplstpVo.getId());
                        testRouteEntity.setFromId(allLines.get(i).get(0));
                        testRouteEntity.setToId(allLines.get(i).get(1));
                        String fromNameFlag = "from";
                        //判断线与top方向是否一致
                        if (topoVoSort.get(j).getFromId().equals(allLines.get(i).get(0))) {

                        } else {
                            fromNameFlag = "to";
                        }
                        if ("from".equals(fromNameFlag)) {
                            testRouteEntity.setFromName(topoVoSort.get(j).getFromName());
                            testRouteEntity.setToName(topoVoSort.get(j).getToName());
                            testRouteEntity.setStartPortId(topoVoSort.get(j).getLinkData().get(0).getStartPortId());
                            testRouteEntity.setStartPort(topoVoSort.get(j).getLinkData().get(0).getStartPort());
                            testRouteEntity.setEndPortId(topoVoSort.get(j).getLinkData().get(0).getEndPortId());
                            testRouteEntity.setEndPort(topoVoSort.get(j).getLinkData().get(0).getEndPort());
                        } else {
                            testRouteEntity.setFromName(topoVoSort.get(j).getToName());
                            testRouteEntity.setToName(topoVoSort.get(j).getFromName());
                            testRouteEntity.setStartPortId(topoVoSort.get(j).getLinkData().get(0).getEndPortId());
                            testRouteEntity.setStartPort(topoVoSort.get(j).getLinkData().get(0).getEndPort());
                            testRouteEntity.setEndPortId(topoVoSort.get(j).getLinkData().get(0).getStartPortId());
                            testRouteEntity.setEndPort(topoVoSort.get(j).getLinkData().get(0).getStartPort());
                        }

                        testRouteEntity.setCreateBy(SecurityUtils.getUsername());
                        testRouteEntity.setCreateTime(DateUtils.getNowDate());
                        testRouteEntity.setUpdateBy(SecurityUtils.getUsername());
                        testRouteEntity.setUpdateTime(DateUtils.getNowDate());
                        busiMplstpRouteGraphMapper.insertBusiMplstpRouteGraph(testRouteEntity);
                        //插入一条与实际方向相反的数据，方便前端展示双向效果
                        testRouteEntity.setId(IdUtils.simpleUUID());

                        if ("from".equals(fromNameFlag)) {
                            testRouteEntity.setFromName(topoVoSort.get(j).getToName());
                            testRouteEntity.setToName(topoVoSort.get(j).getFromName());
                            testRouteEntity.setStartPortId(topoVoSort.get(j).getLinkData().get(0).getEndPortId());
                            testRouteEntity.setEndPortId(topoVoSort.get(j).getLinkData().get(0).getStartPortId());
                            testRouteEntity.setStartPort(topoVoSort.get(j).getLinkData().get(0).getEndPort());
                            testRouteEntity.setEndPort(topoVoSort.get(j).getLinkData().get(0).getStartPort());
                        } else {
                            testRouteEntity.setFromName(topoVoSort.get(j).getFromName());
                            testRouteEntity.setToName(topoVoSort.get(j).getToName());
                            testRouteEntity.setStartPortId(topoVoSort.get(j).getLinkData().get(0).getStartPortId());
                            testRouteEntity.setEndPortId(topoVoSort.get(j).getLinkData().get(0).getEndPortId());
                            testRouteEntity.setStartPort(topoVoSort.get(j).getLinkData().get(0).getStartPort());
                            testRouteEntity.setEndPort(topoVoSort.get(j).getLinkData().get(0).getEndPort());
                        }
                        testRouteEntity.setFromId(allLines.get(i).get(1));
                        testRouteEntity.setToId(allLines.get(i).get(0));

                        testRouteEntity.setCreateTime(DateUtils.getNowDate());
                        busiMplstpRouteGraphMapper.insertBusiMplstpRouteGraph(testRouteEntity);
                    }

                }
            }

        }
    }

    private void insertBusiMplstpRouteGraphCommon2(BusiMplstpVo busiMplstpVo, List<NetElementLinkVo> randomLinks, String routeType) {
        List<List<String>> allLines2 = new ArrayList<>();
        List<NetElementLinkVo> netElementLinkVos = new ArrayList<>();
        allLines2 = GraphUtil.cutRouteToLine(randomLinks.get(0).getElements());
        //将topo图按照线段排序
        for (int i = 0; i < allLines2.size(); i++) {
            for (int j = 0; j < randomLinks.size(); j++) {
                if ((allLines2.get(i).get(0).equals(randomLinks.get(j).getStartElementId()) && allLines2.get(i).get(1).equals(randomLinks.get(j).getEndElementId())) || (allLines2.get(i).get(1).equals(randomLinks.get(j).getStartElementId()) && allLines2.get(i).get(0).equals(randomLinks.get(j).getEndElementId()))) {
                    netElementLinkVos.add(randomLinks.get(j));
                }
            }
        }
        String pathId2 = IdUtils.simpleUUID();
        List<String> oneRoute = new ArrayList<>();
        for (int j = 0; j < netElementLinkVos.size(); j++) {
            oneRoute = netElementLinkVos.get(j).getElements();
            List<List<String>> allLines = GraphUtil.cutRouteToLine(oneRoute);
//            for (int i = 0; i < allLines.size(); i++) {
            //避免重复插入脏数据
            List<BusiMplstpRouteGraph> busiMplstpRouteGraphs = validbusiMplstpRouteGraph2(busiMplstpVo, netElementLinkVos.get(j), pathId2);
            if (CollectionUtils.isEmpty(busiMplstpRouteGraphs)) {
                BusiMplstpRouteGraph testRouteEntity = new BusiMplstpRouteGraph();
                if (StringUtils.isEmpty(testRouteEntity.getId())) {
                    testRouteEntity.setId(IdUtils.simpleUUID());
                }
                testRouteEntity.setRouteType(routeType);
                //线路id  用于区分同一个MplstpId下 的多条线路
                testRouteEntity.setPathId(pathId2);
                //主表主键
                testRouteEntity.setMplstpId(busiMplstpVo.getId());
                testRouteEntity.setFromId(allLines.get(j).get(0));
                testRouteEntity.setToId(allLines.get(j).get(1));
                String fromNameFlag = "from";
                //判断线与top方向是否一致
                 /*   if (netElementLinkVos.get(j).getStartElementId().equals(allLines.get(i).get(0))) {

                    } else {
                        fromNameFlag = "to";
                    }*/
                testRouteEntity.setFromName(netElementLinkVos.get(j).getStartName());
                testRouteEntity.setToName(netElementLinkVos.get(j).getEndName());
                testRouteEntity.setStartPortId(netElementLinkVos.get(j).getStartPortId());
                testRouteEntity.setStartPort(netElementLinkVos.get(j).getStartPort());
                testRouteEntity.setEndPortId(netElementLinkVos.get(j).getEndPortId());
                testRouteEntity.setEndPort(netElementLinkVos.get(j).getEndPort());
                    /*if ("from".equals(fromNameFlag)) {
                        testRouteEntity.setFromName(netElementLinkVos.get(j).getStartName());
                        testRouteEntity.setToName(netElementLinkVos.get(j).getEndName());
                        testRouteEntity.setStartPortId(netElementLinkVos.get(j).getStartPortId());
                        testRouteEntity.setStartPort(netElementLinkVos.get(j).getStartPort());
                        testRouteEntity.setEndPortId(netElementLinkVos.get(j).getEndPortId());
                        testRouteEntity.setEndPort(netElementLinkVos.get(j).getEndPort());
                    } else {
                        testRouteEntity.setFromName(netElementLinkVos.get(j).getEndName());
                        testRouteEntity.setToName(netElementLinkVos.get(j).getStartName());
                        testRouteEntity.setStartPortId(netElementLinkVos.get(j).getEndPortId());
                        testRouteEntity.setStartPort(netElementLinkVos.get(j).getEndPort());
                        testRouteEntity.setEndPortId(netElementLinkVos.get(j).getStartPortId());
                        testRouteEntity.setEndPort(netElementLinkVos.get(j).getStartPort());
                    }*/

                testRouteEntity.setCreateBy(SecurityUtils.getUsername());
                testRouteEntity.setCreateTime(DateUtils.getNowDate());
                testRouteEntity.setUpdateBy(SecurityUtils.getUsername());
                testRouteEntity.setUpdateTime(DateUtils.getNowDate());
                busiMplstpRouteGraphMapper.insertBusiMplstpRouteGraph(testRouteEntity);
                //插入一条与实际方向相反的数据，方便前端展示双向效果
                testRouteEntity.setId(IdUtils.simpleUUID());

               /*     if ("from".equals(fromNameFlag)) {
                        testRouteEntity.setFromName(netElementLinkVos.get(j).getEndName());
                        testRouteEntity.setToName(netElementLinkVos.get(j).getStartName());
                        testRouteEntity.setStartPortId(netElementLinkVos.get(j).getEndPortId());
                        testRouteEntity.setEndPortId(netElementLinkVos.get(j).getStartPortId());
                        testRouteEntity.setStartPort(netElementLinkVos.get(j).getEndPort());
                        testRouteEntity.setEndPort(netElementLinkVos.get(j).getStartPort());
                    } else {
                        testRouteEntity.setFromName(netElementLinkVos.get(j).getStartName());
                        testRouteEntity.setToName(netElementLinkVos.get(j).getEndName());
                        testRouteEntity.setStartPortId(netElementLinkVos.get(j).getStartPortId());
                        testRouteEntity.setEndPortId(netElementLinkVos.get(j).getEndPortId());
                        testRouteEntity.setStartPort(netElementLinkVos.get(j).getStartPort());
                        testRouteEntity.setEndPort(netElementLinkVos.get(j).getEndPort());
                    }*/
                testRouteEntity.setFromName(netElementLinkVos.get(j).getEndName());
                testRouteEntity.setToName(netElementLinkVos.get(j).getStartName());
                testRouteEntity.setStartPortId(netElementLinkVos.get(j).getEndPortId());
                testRouteEntity.setEndPortId(netElementLinkVos.get(j).getStartPortId());
                testRouteEntity.setStartPort(netElementLinkVos.get(j).getEndPort());
                testRouteEntity.setEndPort(netElementLinkVos.get(j).getStartPort());
                testRouteEntity.setFromId(allLines.get(j).get(1));
                testRouteEntity.setToId(allLines.get(j).get(0));

                testRouteEntity.setCreateTime(DateUtils.getNowDate());
                busiMplstpRouteGraphMapper.insertBusiMplstpRouteGraph(testRouteEntity);
            }
//            }
        }

    }

    /**
     * 校验路由图表数据
     * 避免重复插入脏路由图表数据
     */
    List<BusiMplstpRouteGraph> validbusiMplstpRouteGraph2(BusiMplstpVo busiMplstpVo, NetElementLinkVo link, String pathId) {
        BusiMplstpRouteGraph busiMplstpRouteGraphSearchParam = new BusiMplstpRouteGraph();
        busiMplstpRouteGraphSearchParam.setMplstpId(busiMplstpVo.getId());
        busiMplstpRouteGraphSearchParam.setFromId(link.getStartElementId());
        busiMplstpRouteGraphSearchParam.setToId(link.getEndElementId());
        busiMplstpRouteGraphSearchParam.setPathId(pathId);
        return busiMplstpRouteGraphMapper.selectBusiMplstpRouteGraphList(busiMplstpRouteGraphSearchParam);
    }

    /**
     * 校验路由图表数据
     * 避免重复插入脏路由图表数据
     */
    List<BusiMplstpRouteGraph> validbusiMplstpRouteGraph(BusiMplstpVo busiMplstpVo, TopoVo topoVo, String pathId) {
        BusiMplstpRouteGraph busiMplstpRouteGraphSearchParam = new BusiMplstpRouteGraph();
        busiMplstpRouteGraphSearchParam.setMplstpId(busiMplstpVo.getId());
        busiMplstpRouteGraphSearchParam.setFromId(topoVo.getFromId());
        busiMplstpRouteGraphSearchParam.setToId(topoVo.getToId());
        busiMplstpRouteGraphSearchParam.setPathId(pathId);
        return busiMplstpRouteGraphMapper.selectBusiMplstpRouteGraphList(busiMplstpRouteGraphSearchParam);
    }

    /**
     * 插入路由图网元（顺序）对象
     *
     * @param mplstpId
     * @param shortestRoute
     * @param elementMap
     */
    private void insertBusiMplstpRouteElementsBatch(String mplstpId, List<String> shortestRoute, Map<String, String> elementMap, String routeType) {
        for (String elementId : shortestRoute) {
            BusiMplstpRouteElements busiMplstpRouteElements = new BusiMplstpRouteElements();
            busiMplstpRouteElements.setId(IdUtils.simpleUUID());
            busiMplstpRouteElements.setMplstpId(mplstpId);
            busiMplstpRouteElements.setElementId(elementId);
            busiMplstpRouteElements.setElementName(elementMap.get(elementId));
            busiMplstpRouteElements.setRouteType(routeType);
            busiMplstpRouteElements.setCreateBy(SecurityUtils.getUsername());
            busiMplstpRouteElements.setCreateTime(DateUtils.getNowDate());
            busiMplstpRouteElements.setUpdateBy(SecurityUtils.getUsername());
            busiMplstpRouteElements.setUpdateTime(DateUtils.getNowDate());
            busiMplstpRouteElementsMapper.insertBusiMplstpRouteElements(busiMplstpRouteElements);
        }
    }

    /**
     * 更新路由图表信息
     *
     * @param busiMplstpVo
     */
    void updateBusiMplstpRouteGraph(BusiMplstpVo busiMplstpVo) {
        busiMplstpRouteGraphMapper.deleteBusiMplstpRouteGraphByMplstpId(busiMplstpVo.getId());
        busiMplstpRouteElementsMapper.deleteBusiMplstpRouteElementsByMplstpId(busiMplstpVo.getId());
        busiMplstpRouteTableMapper.deleteBusiMplstpRouteTableByMplsId(busiMplstpVo.getId());
        insertBusiMplstpRouteGraph(busiMplstpVo);
    }

    /**
     * @param subLines
     * @return
     */
    public List<List<NetElementLinkVo>> combine(List<List<NetElementLinkVo>> subLines) {
        List<List<NetElementLinkVo>> result = new ArrayList<>();
        if (subLines == null || subLines.isEmpty()) {
            return result;
        }
        backtrack(subLines, 0, new ArrayList<>(), result);
        return result;
    }

    private void backtrack(List<List<NetElementLinkVo>> subLines, int index, List<NetElementLinkVo> currentCombination, List<List<NetElementLinkVo>> result) {
        if (index == subLines.size()) {
            result.add(new ArrayList<>(currentCombination));
            return;
        }
        List<NetElementLinkVo> currentList = subLines.get(index);
        for (NetElementLinkVo element : currentList) {
            currentCombination.add(element);
            backtrack(subLines, index + 1, currentCombination, result);
            currentCombination.remove(currentCombination.size() - 1);
        }
    }
}
